import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class Teaks extends Script implements ServerMessageListener,
		PaintListener {

	int loops = 0;
	int logs = 0;
	int fxp = 0;
	boolean dropping = false;
	boolean cutting = false;
	boolean adze = false;
	public final RSTile treetile = new RSTile(2774, 2698);
	public long startTime = System.currentTimeMillis();
	RSObject tree;

	public void antiBan() {

		switch (random(0, 150)) {
		case 0:
			moveMouse(random(0, 600), random(0, 600));
			wait(random(400, 600));
			break;
		case 1:
			moveMouse(random(0, 600), random(0, 600));
			wait(random(400, 600));
			break;
		case 2:
			moveMouse(random(0, 600), random(0, 600));
			wait(random(400, 600));
			break;
		case 3:
			moveMouse(random(0, 600), random(0, 600));
			wait(random(400, 600));
			break;
		case 4:
			setCameraRotation(random(1, 300));
			wait(random(400, 600));
			break;
		case 5:
			setCameraRotation(random(1, 300));
			wait(random(400, 600));
			break;
		case 7:
			setCameraAltitude(true);
			wait(random(400, 600));
			break;
		case 8:
			openTab(Constants.TAB_INVENTORY);
			wait(random(400, 600));
			break;
		case 9:
			openTab(Constants.TAB_STATS);
			wait(random(400, 600));
			break;
		}
	}

	@Override
	public String getAuthor() {
		return "Atomic Sky";
	}

	@Override
	public String getName() {
		return "Teaks";
	}

	@Override
	public String getScriptCategory() {
		return "Woodcutting";
	}

	@Override
	public String getScriptDescription() {
		return "<html>\n"
				+ "<head></head>\n"
				+ "<body bgcolor=\"black\" text=\"white\">\n"
				+ "<center>"
				+ "<h2>"
				+ getName()
				+ "</h2>"
				+ "</center>\n"
				+ "<b> Author:</b> "
				+ getAuthor()
				+ ", edited by DrFrijole<br />"
				+ "<b> Version:</b> "
				+ getVersion()
				+ "<b><br> Start near the teak trees on Ape Atoll island."
				+ "<br>"
				+ "<br>"
				+ "<br>"
				+ "<input type=\"checkbox\" name=\"doadze\" value=\"true\"><B>Using Inferno Adze? </b>"
				+ "<b><br><br><br> <i>Note: make sure to be in monkey form. </body>\n"
				+ "</html>";
	}

	@Override
	public double getVersion() {
		return 1.4;
	}

	@Override
	public int loop() {

		if (dropping) {

			if (getInventoryCount(6333) > 0) {
				dropAllExcept(1349, 1353, 1355, 1357, 1359, 1361, 6739, 13661);
				wait(random(200, 400));
			}

			else if (getInventoryCount(6333) == 0) {
				dropping = false;
				cutting = false;
			}
		}

		else if (!dropping) {

			if (distanceTo(treetile) >= 10) {
				walkTileMM(treetile);
				wait(random(800, 1200));
			}

			else if (distanceTo(treetile) < 10) {

				if (!cutting) {
					setRun(true);
					tree = getNearestObjectByID(9036);

					if (tree != null) {

						if (!tileOnScreen(tree.getLocation())) {
							turnToTile(tree.getLocation());
							wait(random(200, 400));
						}

						else if (tileOnScreen(tree.getLocation())) {
							atObject(tree, "Chop");
							wait(random(800, 1200));
						}
					}
				}

				else if (cutting) {
					final RSTile interacting = tree.getLocation();
					loops++;

					if (loops < 200) {
						if (getObjectAt(interacting).getID() != 9036) {
							cutting = false;
							loops = 0;
						}

						else if (getObjectAt(interacting).getID() == 9036) {
							antiBan();
						}
					}

					else if (loops > 200) {
						log("Something weird happened. Reseting.");
						cutting = false;
						loops = 0;
					}
				}
			}
		}

		return random(100, 150);
	}

	@Override
	public void onFinish() {
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {
			if (fxp == 0) {
				fxp = skills.getCurrentSkillExp(Constants.STAT_FIREMAKING);
			}
			final int xp = skills.getCurrentSkillExp(Constants.STAT_FIREMAKING);
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;
			final int x = 388;
			final int y = 20;
			g.setColor(Color.red);
			g.drawString("Teaks v1.2", x, y);
			g.setColor(Color.white);
			g.drawString("Runtime: " + hours + ":" + minutes + ":" + seconds
					+ "", x, y + 15);
			g.drawString("Cut: " + logs / 1, x, y + 30);
			g.drawString("WC Exp: " + logs / 1 * 85 + " ("
					+ skills.getPercentToNextLevel(8) + "%)", x, y + 45);
			if (adze) {
				g.drawString("FM Exp: " + (xp - fxp) + " ("
						+ skills.getPercentToNextLevel(11) + "%)", x, y + 60);
			}
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		adze = args.get("doadze") != null ? true : false;
		return true;
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		if (e.getMessage().equalsIgnoreCase(
				"You swing your hatchet at the tree.")) {
			cutting = true;
		}

		if (e.getMessage().equalsIgnoreCase("You get some teak logs.")) {
			logs++;
		}

		if (e.getMessage().equalsIgnoreCase(
				"Your inventory is too full to hold any more logs.")) {
			dropping = true;
		}
	}

	public void setCameraRotation2(int degrees) {
		final char left = 37;
		final char right = 39;
		char whichDir = left;
		int start = getCameraAngle();

		if (start < 180) {
			start += 360;
		}

		if (degrees < 180) {
			degrees += 360;
		}

		if (degrees > start) {

			if (degrees - 180 < start) {
				whichDir = right;
			}
		}

		else if (start > degrees) {

			if (start - 180 >= degrees) {
				whichDir = right;
			}
		}
		degrees %= 360;

		Bot.getInputManager().pressKey(whichDir);
		int timeWaited = 0;

		while (getCameraAngle() > degrees + 5 || getCameraAngle() < degrees - 5) {
			wait(10);
			timeWaited += 10;

			if (timeWaited > 500) {
				final int time = timeWaited - 500;

				if (time == 0) {
					Bot.getInputManager().pressKey(whichDir);
				} else if (time % 40 == 0) {
					Bot.getInputManager().pressKey(whichDir);
				}
			}
		}
		Bot.getInputManager().releaseKey(whichDir);
	}

	@Override
	public void turnToTile(final RSTile tile) { // I can't remember
		int angle = getAngleToCoordinates(tile.getX(), tile.getY()); // where I
		// took
		// this
		angle = angle + random(-20, 20); // from, but credits
		// to whoever made it!
		if (angle < 0) {
			angle = 360 + angle;
		}

		if (angle >= 360) {
			angle -= 360;
		}
		setCameraRotation2(angle);
	}
}